/* Includes: */
#if defined(OSC_BUILD_SKIN_EDITOR)
# include "gui/forms/osc_skin_editor_form.h"
#else
# include "gui/forms/osc_main_window_form.h"
#endif
#include "gui/skinmanager/osc_global_skin_manager.h"
#include "app/osc_application.h"
#include "gui/components/osc_plotter.h"
#include "plugin/osc_plugin_manager.h"
//#include "osc_main_form.h"
#include "xml_parser/xml_data.hpp"

OSC_USE_NAMESPACE
OSC_USE_MODULE( appl )
OSC_USE_MODULE( plugin )
//OSC_USE_MODULE( device )


XmlData g_configData;

int main ( int argc, char *argv[] )
{
    OscApplication app( argc, argv );

    OscSkinManager_SetSkinsExt( "osk" );
    OscSkinManager_SetSkinsPath( "skins" );

#if defined(OSC_BUILD_SKIN_EDITOR)
    OscSkinEditorForm window;
    OscSkinManager_ShowSkinedMainWindowMaximized( &window );
    return app.exec();
#else
    if ( ! g_configData.loadFile( "config.xml" ) ) {
        // error
    }
    XmlElement confSec = g_configData.dataElement().firstElementByPath( "/config/skins/ext" );
    if ( confSec ) {
        OscSkinManager_SetSkinsExt( confSec.childValue() );
    }

    confSec = g_configData.dataElement().firstElementByPath( "/config/skins/path" );
    if ( confSec ) {
        OscSkinManager_SetSkinsPath( confSec.childValue() );
    }

    const char* confStr = "black";
    confSec = g_configData.dataElement().firstElementByPath( "/config/skins/skin" );
    if ( confSec ) {
        confStr = confSec.childValue();
    }
    OscSkinManager_SetCurrentSkin( confStr );

    confStr = "plugins";
    confSec = g_configData.dataElement().firstElementByPath( "/config/plugins/path" );
    if ( confSec ) {
        confStr = confSec.childValue();
    }
    g_pluginManager.setPluginsPath( confStr );
    vector<OscPluginFileInfo> plugList = g_pluginManager.getPluginsList();

    qDebug( "Plugins:" );
    for ( size_t listIndex = 0; listIndex < plugList.size(); ++listIndex ) {
        OscPluginFileInfo curInfo = plugList.at( listIndex );
        qDebug( "%d. %s (corrupted: %d)",
                curInfo.plugId, curInfo.fileName.c_str(), curInfo.corrupted );
        if ( curInfo.corrupted ) {
            qDebug( "   corrupt type: %d", curInfo.corruptType );
        } else {
            qDebug( "   name: %s", curInfo.info.pluginName.c_str() );
        }
    }

    confStr = "osc_drv_com.dll";
    confSec = g_configData.dataElement().firstElementByPath( "/config/plugins/driver" );
    if ( confSec ) {
        confStr = confSec.childValue();
    }
    g_pluginManager.setPluginToUsageByFileName( confStr );

    confStr = "osc_prot_olegdevice.dll";
    confSec = g_configData.dataElement().firstElementByPath( "/config/plugins/protocol" );
    if ( confSec ) {
        confStr = confSec.childValue();
    }
    g_pluginManager.setPluginToUsageByFileName( confStr );

    OscMainWindowForm window;
    OscSkinManager_ShowSkinedMainWindowMaximized( &window );
    app.setApplicationName( window.windowTitle().toAscii().constData() );
    int res = app.exec();

    //delete window;

    /* Save configuration to file. */
    g_configData.saveFile( "config.xml" );

    return res;
#endif
}
